package algorithms.leaning.class26;

import common.util.MyUtil;

/**
 * 快速求出X的N次幂
 *
 * @author guichang
 * @date 2021/6/28
 */

@SuppressWarnings("all")
public class Code3_其他_2的N次幂 {

    public static void main(String[] args) {
        for (int i = 2; i <= 8; i++) {
            MyUtil.printf("Math.pow({}, 5) = {}", i, powerOfX(i, 5));
        }
    }

    /**
     * X的N次方
     */
    public static long powerOfX(int X, int N) {
        long res = 1;
        while (N != 0) {
            if ((N & 1) == 1) {
                res *= X;
            }

            X *= X;
            N >>= 1;
        }
        return res;
    }

    public static long powerOfX2(int X, int N) {
        long res = 1;
        for (int i = 0; i < N; i++) {
            res *= X;
        }
        return res;
    }
}